/*
 * Created by JFormDesigner on Tue May 25 23:01:54 CST 2021
 */

package com.dfrz.bookstore.view;

import java.awt.event.*;
import com.dfrz.bookstore.entry.*;
import com.dfrz.bookstore.service.impl.BookServiceImpl;
import com.dfrz.bookstore.service.impl.OrderItemServiceImpl;
import com.dfrz.bookstore.service.impl.OrderServiceImpl;
import com.sun.xml.internal.bind.v2.TODO;


import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class OrderItemView extends JFrame {
    private Order order;
    private User user;
    private Admin admin;
    OrderItemServiceImpl orderItemService = new OrderItemServiceImpl();
    BookServiceImpl bookService = new BookServiceImpl();
    public static void main(String[] args) {
        Order order = new Order();
        order.setId(26);
        User user = new User();
        user.setId(2);
        OrderItemView orderItemView1 = new OrderItemView(order,user);
        orderItemView1.setVisible(true);
    }
    public OrderItemView(Order order,User user) {
        initComponents();
        this.order = order;
        this.user = user;
        showOrderItem(order);
        setTitle(user.getName()+"管理自己的订单");

    }

    public OrderItemView(Order order,Admin admin) {
        initComponents();
        this.order = order;
        this.admin = admin;
        showOrderItem(order);
        setTitle("管理员操作");
        ship.setVisible(true);
        ship.addActionListener(e -> ShipActionPerformed(e,order));
    }


    public void showOrderItem(Order order){
        OrderItem orderItem = new OrderItem();
        orderItem.setOrder_id(order.getId());
        /*设置滚动条滚动量*/
        scrollPane1.getVerticalScrollBar().setUnitIncrement(15);
        List<OrderItem> orderItemList = orderItemService.findObjByObj(orderItem);
        for (int i = 0; i < orderItemList.size(); i++) {
            OrderItem orderItem1 = orderItemList.get(i);
            Book book = new Book();
            book.setId(orderItem1.getBook_id());
            Book book1 = bookService.findBookByBook(book).get(0);
            /*调用动态添加订单页面*/
            addOrderItem(orderItemList.get(i),book1,i);
        }


        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < panel1.getComponentCount(); i++) {
                Rectangle bounds = panel1.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = panel1.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            panel1.setMinimumSize(preferredSize);
            panel1.setPreferredSize(preferredSize);
        }

    }

    /**管理员发货
     * @param e
     * @param order
     */
    private void ShipActionPerformed(ActionEvent e, Order order) {
        System.out.println("11");
        order.setStatus(1);
        System.out.println(order);
        orderService.upDataOrder(order);
        AdminJFrame adminJFrame = new AdminJFrame(admin,new Book());
        adminJFrame.getClientPane1().setSelectedIndex(1);
        adminJFrame.setVisible(true);
        dispose();

    }

    public void addOrderItem(OrderItem orderItem,Book book,int i){

        //---- IDLabel 隐藏控件 方便拿到订单项的id ----
        JLabel IDLabel = new JLabel();
        IDLabel.setText(String.valueOf(orderItem.getId()));
        panel1.add(IDLabel);
        IDLabel.setBounds(135, 10+(i*160), 1, 1);

        //---- IDLabel 隐藏控件 方便拿到订单的id ----
        JLabel OrderId = new JLabel();
        OrderId.setText(String.valueOf(orderItem.getOrder_id()));
        panel1.add(IDLabel);
        IDLabel.setBounds(135, 10+(i*160), 1, 1);

        //---- label1 ----
        ImageIcon icon = null;
        try {
            icon = new ImageIcon(new URL(book.getImg_src()));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        icon.setImage(icon.getImage().getScaledInstance(115,120,Image.SCALE_DEFAULT));
        JLabel label1 = new JLabel();
        label1.setIcon(icon);
        panel1.add(label1);
        label1.setBounds(10, 0+(i*160), 115, 140);

        //---- label2 ----
        JLabel label2 = new JLabel();
        label2.setText("\u4e66\u540d:");
        panel1.add(label2);
        label2.setBounds(135, 10+(i*160), 40, 17);

        //---- label3 ----
        JLabel label3 = new JLabel();
        label3.setText("\u6570\u91cf:");
        panel1.add(label3);
        label3.setBounds(290, 60+(i*160), 40, 17);

        //---- label4 ----
        JLabel label4 = new JLabel();
        label4.setText("\u8ba2\u5355\u65f6\u95f4:");
        panel1.add(label4);
        label4.setBounds(285, 10+(i*160), 60, 17);

        //---- label5 ----
        JLabel label5 = new JLabel();
        label5.setText("\u672c\u9879\u5c0f\u8ba1");
        panel1.add(label5);
        label5.setBounds(416, 61+(i*160), 60, 17);

        //---- label6 ----
        JLabel label6 = new JLabel();
        label6.setText("单价:");
        panel1.add(label6);
        label6.setBounds(138, 60+(i*160), 50, 17);

        //---- bookName ----
        JLabel bookName = new JLabel();
        bookName.setText(book.getName());
        panel1.add(bookName);
        bookName.setBounds(185, 10+(i*160), 70, 17);

        //---- price ----
        JLabel price = new JLabel();
        price.setText(String.valueOf(book.getPrice()));
        panel1.add(price);
        price.setBounds(185, 60+(i*160), 45, 17);

        //---- creatTime ----
        JLabel creatTime = new JLabel();
        creatTime.setText(String.valueOf(orderItem.getCreate_time()));
        panel1.add(creatTime);
        creatTime.setBounds(350, 10+(i*160), 160, 17);

        //---- num ----
        //计算数量
        double n = (orderItem.getPrice_sum()*10)/(book.getDiscount()*book.getPrice());
        String format = new DecimalFormat("#####").format(n);
        JLabel num = new JLabel();
        num.setText(format);
        panel1.add(num);
        num.setBounds(340, 60+(i*160), 45, 17);

        //---- priceSum ----
        JLabel priceSum = new JLabel();
        priceSum.setText(String.valueOf(orderItem.getPrice_sum()));
        panel1.add(priceSum);
        priceSum.setBounds(480, 60+(i*160), 70, 17);

        //用户才有退货 删除订单按钮  管理员没有
        if (user!=null){
            //-----删除订单按钮---------
            JButton delOrderItem = new JButton();
            delOrderItem.setText("\u5220\u9664\u8ba2\u5355\u9879");
            delOrderItem.addActionListener(e -> delOrderItemActionPerformed(e,Integer.valueOf(IDLabel.getText())));
            panel1.add(delOrderItem);
            delOrderItem.setBounds(new Rectangle(new Point(490, 30+(i*160)), delOrderItem.getPreferredSize()));

            //-----退货订单按钮---------
            JButton returnOfGoods = new JButton();
            returnOfGoods.setText("退货");
            returnOfGoods.addActionListener(e -> returnOfGoodsActionPerformed(e,Integer.valueOf(OrderId.getText()),3));
            panel1.add(returnOfGoods);
            returnOfGoods.setBounds(new Rectangle(new Point(490, 80+(i*160)), delOrderItem.getPreferredSize()));

            //-----确认收货按钮---------
            JButton receipt = new JButton();
            receipt.setText("收货");
            receipt.addActionListener(e -> returnOfGoodsActionPerformed(e,Integer.valueOf(OrderId.getText()),2));
            panel1.add(receipt);
            receipt.setBounds(new Rectangle(new Point(400, 80+(i*160)), delOrderItem.getPreferredSize()));
        }
              //---- border ----
        JLabel border = new JLabel();
        border.setText("--------------------------------------------------------------------------------------------------------");
        panel1.add(border);
        border.setBounds(25, 120+(i*160), 535, 17);


    }
    OrderServiceImpl orderService = new OrderServiceImpl();

    //确认收货 和 退货按监听事件
    private void returnOfGoodsActionPerformed(ActionEvent e,int id,int status) {
        Order order = new Order();
        order.setId(id);
        order.setStatus(status);//修改订单为退货状态
        orderService.upDataOrder(order);
    }
    //删除订单监听事件
    private void delOrderItemActionPerformed(ActionEvent e,int id) {
        orderItemService.delectOrderItemById(id);
    }

    private void thisWindowClosed(WindowEvent e) {
        // TODO add your code here

    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        scrollPane1 = new JScrollPane();
        panel1 = new JPanel();
        ship = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== scrollPane1 ========
        {

            //======== panel1 ========
            {
                panel1.setLayout(null);

                //---- ship ----
                ship.setText("\u5168\u90e8\u53d1\u8d27");
                ship.setVisible(false);
                ship.addActionListener(e -> button1ActionPerformed(e));
                panel1.add(ship);
                ship.setBounds(new Rectangle(new Point(500, 450), ship.getPreferredSize()));

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel1.getComponentCount(); i++) {
                        Rectangle bounds = panel1.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel1.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel1.setMinimumSize(preferredSize);
                    panel1.setPreferredSize(preferredSize);
                }
            }
            scrollPane1.setViewportView(panel1);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(0, 0, 595, 495);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JScrollPane scrollPane1;
    private JPanel panel1;
    private JButton ship;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
